<%@include file="Common.jsp"%>
<%!static final String sFileName = "Search.jsp";%>
<%@page import="org.apache.commons.httpclient.HttpStatus"%>
<%@page import="org.apache.commons.httpclient.methods.GetMethod"%>
<%@page import="org.apache.commons.httpclient.HttpClient"%>

<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.DataInputStream"%>
<%@page import="java.net.URL"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Effective Navigation of Query Results Based on Concept
	Hierarchies</title>


<%!public static int pagec = 1;

	String sitename = "google.com", query = "harry potter";
	String qs;
	String part_two = "";
	StringBuffer file = new StringBuffer();%>
<body>
		
		<%
			boolean bDebug = false;

			String sAction = getParam(request, "FormAction");
			String sForm = getParam(request, "FormName");
			String sSearchErr = "";
			String sItemsErr = "";

			java.sql.Connection conn = null;
			java.sql.Statement stat = null;
			String sErr = loadDriver();
			conn = cn();
			stat = conn.createStatement();
			if (!sErr.equals("")) {
				try {
					out.println(sErr);
				} catch (Exception e) {
				}
			}
		%>
		<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#example').dataTable( {
					
				} );
				
				$("#browser").treeview({
					toggle: function() {
						console.log("%s was toggled.", $(this).find(">span").text());
					}
				});

			} );
		</script>
		


			<jsp:include page="Header.jsp" flush="true" />
			


				<h2>Enter Search Term</h2>
				<%
					Search_Show(request, response, session, out, sSearchErr, sForm,
							sAction, conn, stat);
				%>
				<table >
					<tr>
						<td width="70%">Search Results:
							<table border="1" id="example"
								class="table table-striped table-bordered table-condensed">
								<thead>
									<tr>
										<th>Name</th>
										<th>Link</th>
										<th>Category</th>
										<th>Description</th>
									</tr>
								</thead>
								<tbody>
									<%
										Items_Show(request, response, session, out, sItemsErr, sForm,
												sAction, conn, stat);
									%>

								</tbody>
							</table>
						</td>
						<td>
							
									<p align="left">
										<font color="#FF0000"><strong>Query Results
												navigation tree Hierarchies1</strong></font>
									</p>
									<br> <br>
									<p align="left">
										<font color="#3366FF"> <%!int noofoption = 0;
	String[] cats = new String[100];%> <%
 	java.sql.Statement stat2 = null;
 	java.sql.ResultSet rs = null;
 	java.sql.ResultSet rs1 = null;
 	java.sql.ResultSet rs2 = null;

 	conn = cn();
 	stat = conn.createStatement();
 	stat2 = conn.createStatement();
 	if (!sErr.equals("")) {
 		try {
 			out.println(sErr);
 		} catch (Exception e) {
 		}
 	}

 	try {
 		rs = stat
 				.executeQuery("Select count(category_id) as counts from categories");
 		if (rs.next()) {
 			noofoption = Integer.parseInt(rs.getString("counts"));

 		}
 		//out.print("size "+noofoption);
 	} catch (Exception e) {
 		out.print(e.getMessage());
 	}

 	///
 	try {

 		rs1 = stat.executeQuery("Select * from categories");
 		//int i=0;
 		while (rs1.next()) {
 			//   cats[i]=new String();
 			//   cats[i]="hi";
 			//out.print(rs1.getString(2));
 			//cats[i]=
 			//for(int i=0;i<noofoption;i++)
 			//{
 			String id = rs1.getString(1);
 			String sap = rs1.getString(2);
 			//out.print(id);
 			out.print("<input type=\"checkbox\" name=\"id\" value="
 					+ sap + ">" + sap + "<BR>");
 			rs2 = stat2
 					.executeQuery("Select url,name from links where category_id="
 							+ id + " and approved=1");

 			while (rs2.next()) {
 				String ssa = rs2.getString(1);
 				String sa = rs2.getString(2);

 				out.print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"id\" value="
 						+ ssa + " >" + sa + "<BR>");
 			}
 			out.print("<br>");
 			//}
 			//i++;

 		}
 	} catch (Exception e) {
 		out.print(" aa" + e.getMessage());
 	}
 %>
										</font>
									</p>
									<p>
										<input type="submit" name="next" id="next"
											value="Next Set Of results" class="btn btn-primary" />
									</p>

								
	</td>
	</tr>
	</table>
	
	
	
</body>




</html>
<%!void Search_Show(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response,
			javax.servlet.http.HttpSession session,
			javax.servlet.jsp.JspWriter out, String sSearchErr, String sForm,
			String sAction, java.sql.Connection conn, java.sql.Statement stat)
			throws java.io.IOException {
		try {

			String flddescription = "";

			String sSQL = "";
			String transitParams = "";
			String sQueryString = "";
			String sPage = "";

			out.println("     <form method=\"get\" action=\"Search.jsp\" name=\"Search\">\n     <tr>");
			// Set variables with search parameters

			flddescription = getParam(request, "description");

			// Show fields

			out.print("<input type=\"text\"  name=\"description\" maxlength=\"\" value=\""
					+ toHTML(flddescription) + "\" size=\"\">");

			out.println("     <input type=\"submit\" value=\"Search\"  class=\"btn btn-primary\"/>");
			out.println("        </form>");
			out.println("");
		} catch (Exception e) {
			out.println(e.toString());
		}
	}

	void Items_Show(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response,
			javax.servlet.http.HttpSession session,
			javax.servlet.jsp.JspWriter out, String sItemsErr, String sForm,
			String sAction, java.sql.Connection conn, java.sql.Statement stat)
			throws java.io.IOException {

		String sWhere = "";
		int iCounter = 0;
		int iPage = 0;
		boolean bIsScroll = true;
		boolean hasParam = false;
		String sOrder = "";
		String sSQL = "";
		String transitParams = "";
		String sQueryString = "";
		String sPage = "";
		int RecordsPerPage = 20;
		String sSortParams = "";
		String formParams = "";

		String pcategory_id = "";
		String pdescription = "";

		transitParams = "category_id="
				+ toURL(getParam(request, "category_id")) + "&description="
				+ toURL(getParam(request, "description")) + "&";
		formParams = "category_id=" + toURL(getParam(request, "category_id"))
				+ "&description=" + toURL(getParam(request, "description"))
				+ "&";
		// Build WHERE statement

		//-- Check category_id parameter and create a valid sql for where clause

		pcategory_id = getParam(request, "category_id");
		if (!isNumber(pcategory_id)) {
			pcategory_id = "";
		}

		if (pcategory_id != null && !pcategory_id.equals("")) {

			hasParam = true;
			sWhere += "l.category_id=" + pcategory_id;
		}

		//-- Check description parameter and create a valid sql for where clause

		pdescription = getParam(request, "description");
		if (pdescription != null && !pdescription.equals("")) {

			if (!sWhere.equals(""))
				sWhere += " and ";
			hasParam = true;
			sWhere += "l.description like '% "
					+ replace(pdescription, "'", "''") + " %'";
			
			sWhere += " or ";
			
			sWhere += "l.description like '"
					+ replace(pdescription, "'", "''") + " %'";
			
			sWhere += " or ";
			
			sWhere += "l.description like '% "
					+ replace(pdescription, "'", "''") + "'";
		}

		if (hasParam) {
			sWhere = " AND (approved=1) AND (" + sWhere + ")";
		} else {
			sWhere = " AND approved=1";
		}
		// Build ORDER statement
		sOrder = " order by l.name Asc";
		String sSort = getParam(request, "FormItems_Sorting");
		String sSorted = getParam(request, "FormItems_Sorted");
		String sDirection = "";
		String sForm_Sorting = "";
		int iSort = 0;
		try {
			iSort = Integer.parseInt(sSort);
		} catch (NumberFormatException e) {
			sSort = "";
		}
		if (iSort == 0) {
			sForm_Sorting = "";
		} else {
			if (sSort.equals(sSorted)) {
				sSorted = "0";
				sForm_Sorting = "";
				sDirection = " DESC";
				sSortParams = "FormItems_Sorting=" + sSort
						+ "&FormItems_Sorted=" + sSort + "&";
			} else {
				sSorted = sSort;
				sForm_Sorting = sSort;
				sDirection = " ASC";
				sSortParams = "FormItems_Sorting=" + sSort
						+ "&FormItems_Sorted=" + "&";
			}

			if (iSort == 1) {
				sOrder = " order by l.name" + sDirection;
			}
			if (iSort == 2) {
				sOrder = " order by l.url" + sDirection;
			}
			if (iSort == 3) {
				sOrder = " order by c.category_desc" + sDirection;
			}
			if (iSort == 4) {
				sOrder = " order by l.description" + sDirection;
			}
		}

		// Build full SQL statement

		sSQL = "select l.category_id as l_category_id, "
				+ "l.description as l_description, "
				+ "l.link_id as l_link_id, " + "l.name as l_name, "
				+ "l.url as l_url, " + "c.category_id as c_category_id, "
				+ "c.category_desc as c_category_desc "
				+ " from links l, categories c"
				+ " where c.category_id=l.category_id  ";

		sSQL = sSQL + sWhere + sOrder;

		String sNoRecords = "     <tr>\n      <td colspan=\"4\" " + styleDataTD
				+ "><font " + styleDataFONT
				+ ">No records</font></td>\n     </tr>";

		String tableHeader = "";
		tableHeader = "     <tr>\n      <td " + styleColumnTD + "><a href=\""
				+ sFileName + "?" + formParams
				+ "FormItems_Sorting=1&FormItems_Sorted=" + sSorted
				+ "&\"><font " + styleColumnFONT
				+ ">Name</font></a></td>\n      <td " + styleColumnTD
				+ "><a href=\"" + sFileName + "?" + formParams
				+ "FormItems_Sorting=2&FormItems_Sorted=" + sSorted
				+ "&\"><font " + styleColumnFONT
				+ ">URL</font></a></td>\n      <td " + styleColumnTD
				+ "><a href=\"" + sFileName + "?" + formParams
				+ "FormItems_Sorting=3&FormItems_Sorted=" + sSorted
				+ "&\"><font " + styleColumnFONT
				+ ">Category</font></a></td>\n      <td " + styleColumnTD
				+ "><a href=\"" + sFileName + "?" + formParams
				+ "FormItems_Sorting=4&FormItems_Sorted=" + sSorted
				+ "&\"><font " + styleColumnFONT
				+ ">Description</font></a></td>\n     </tr>";

		try {

		} catch (Exception e) {
		}

		try {
			// Select current page
			iPage = Integer.parseInt(getParam(request, "FormItems_Page"));
		} catch (NumberFormatException e) {
			iPage = 0;
		}

		if (iPage == 0) {
			iPage = 1;
		}
		RecordsPerPage = 100;
		try {
			java.sql.ResultSet rs = null;
			// Open recordset
			rs = openrs(stat, sSQL);
			iCounter = 0;
			absolute(rs, (iPage - 1) * RecordsPerPage + 1);
			java.util.Hashtable rsHash = new java.util.Hashtable();
			String[] aFields = getFieldsName(rs);

			// Show main table based on recordset
			while ((iCounter < RecordsPerPage) && rs.next()) {

				getRecordToHash(rs, rsHash, aFields);
				String fldcategory_id = (String) rsHash.get("c_category_desc");
				String flddescription = (String) rsHash.get("l_description");
				String fldname = (String) rsHash.get("l_name");
				String fldevent_url = (String) rsHash.get("l_url");

				out.println("     <tr>");

				out.print("      <td " + styleDataTD + ">");
				out.print("<font " + styleDataFONT + ">" + toHTML(fldname)
						+ "&nbsp;</font>");
				out.println("</td>");
				out.print("      <td " + styleDataTD + ">");
				out.print("<a href=\"" + (String) rsHash.get("l_url")
						+ "\"><font " + styleDataFONT + ">"
						+ toHTML(fldevent_url) + "</font></a>");

				out.println("</td>");
				out.print("      <td " + styleDataTD + ">");
				out.print("<font " + styleDataFONT + ">"
						+ toHTML(fldcategory_id) + "&nbsp;</font>");
				out.println("</td>");
				out.print("      <td " + styleDataTD + ">");
				out.print("<font " + styleDataFONT + ">" + flddescription
						+ "&nbsp;</font>");
				out.println("</td>");
				out.println("     </tr>");

				iCounter++;
			}
		} catch (Exception e) {
			out.println(e.toString());
		}
	}%>